import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
// import {FormControl, FormGroup} from '@angular/forms';
import { GetOrginizationService } from '../../../services/get-orginization.service'
// import EventEmitter = NodeJS.EventEmitter;

@Component({
  selector: 'app-org-select',
  templateUrl: './org-select.component.html',
  styleUrls: ['./org-select.component.css']
  // providers:[FormControl,FormGroup]
})
export class OrgSelectComponent implements OnInit {

  constructor(
    private getOrg:GetOrginizationService
  ) { }

  @Input() ifDevice:boolean
  @Input() stationId:number
  @Input() fanIndex:number

  @Output() freshStation = new EventEmitter<any>();
  @Output() freshSet = new EventEmitter<any>()
  @Output() freshCount = new EventEmitter<any>()

  public branches:any[]=[]
  public selectBranch:any;
  // form :any
  public stations:any[]=[]
  public selectStation:any

  public sets:any[]=[]
  public selectSet:any

  ngOnInit() {
    // console.log(this.stationId)
    this.getOrg.getOrg()
      .subscribe(
        org=>{
          org.forEach(branch=>{
            if(branch.stationSet){
              this.branches.push({
                name:branch.name,
                id:branch.id
              })
            }
          })
          this.getOrg.getOrgById(this.stationId)
            .subscribe(station=>{
              this.branches.forEach(branch=>{
                if(branch.id===station.fatherId){
                  this.selectBranch=branch
                }
              })
              if(this.ifDevice && station.wtSet){
                this.sets=station.wtSet;
                this.selectSet=this.sets[this.fanIndex-1]//风机索引从0开始 所以要减一
              }
              this.getOrg.getOrgById(this.selectBranch.id)
                .subscribe(selectBranch=>{
                  // console.log(selectBranch)
                  selectBranch.stationSet.forEach(curStation=>{
                    if(curStation.wtSet)
                      this.stations.push({
                        parentId:station.fatherId,
                        name:curStation.nameForShort,
                        id:curStation.id
                      })
                  });
                  this.stations.forEach(st=>{
                    if(st.id===this.stationId){
                      this.selectStation=st
                    }
                  });
                })
            })
        }
      )
    // for (let i=0;i<200;i++){
    //   this.branches.push({
    //     name:i+'号风场',
    //     id:i
    //   })
    // }
    // this.selectBranch=this.branches[5]
  }



  branchChange(selectBranch:any){
    this.getOrg.getOrgById(selectBranch.id).subscribe(
      branch=>{
        if(branch.stationSet){
          this.stations=[];
          branch.stationSet.forEach(station=>{
            if(station.wtSet)
              this.stations.push({
                parentId:station.fatherId,
                name:station.nameForShort,
                id:station.id
              })
          })
          // this.selectStation=this.stations[0];
          this.stationChange(this.stations[0]);
        }
      }
    )
  }
  stationChange(selectStation:any){
    this.selectStation=selectStation;
    this.freshStation.emit(this.selectStation)
    this.getOrg.getOrgById(selectStation.id)
      .subscribe(station=>{
        this.sets=station.wtSet;
        this.freshCount.emit(this.sets.length)
        this.setChange(this.sets[0])
      })
  }
  setChange(selectSet:any){
    this.selectSet=selectSet;
    this.freshSet.emit(selectSet)
  }
}
